package com.byit.controller;
/**
 * Created by marker on 2018/3/6.
 */

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.constant.DefaultValue;
import com.byit.domain.AppApplication;
import com.byit.domain.vo.AboutUsVo;
import com.byit.domain.vo.ApplicationFigure;
import com.byit.domain.vo.ContactUsVo;
import com.byit.domain.vo.IntroduceVo;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;


/**
 * 配置接口
 **/
@Slf4j
@RestController
@RequestMapping("/api/config")
public class ConfigController {

    /**
     * ConfigService
     */
    @Autowired
    private ConfigService configService;

    /**
     * 联系我们
     * @return
     * @author wumin
     */
    @Log(title = "联系我们", operate = OperatorType.DATA_SAVEORUPDATE, channel = ChannelType.WEB_INTER)
    @PostMapping("/setContactUs")
    public Object setContactUs(
            HttpSession session, ContactUsVo contactUsVo) {

        configService.setContactUs(contactUsVo);
        return MessageResult.success();
    }

    /**
     * 联系我们
     * @return
     * @author wumin
     */
    @GetMapping("/getContactUs")
    public Object getContactUs(
            HttpSession session, String configKey) {

        ContactUsVo contactUsVo = configService.getContactUs(ContactUsVo.CONFIG_KEY);
        return MessageResult.success(contactUsVo);
    }


    /**
     * app图文介绍添加
     * @return
     * @author wumin
     */
    @Log(title = "app图文介绍", operate = OperatorType.DATA_SAVEORUPDATE, channel = ChannelType.WEB_INTER)
    @PostMapping("/setIntroduce")
    public Object setIntroduce(
            HttpSession session, IntroduceVo IntroduceVo) {

        configService.setIntroduce(IntroduceVo);
        return MessageResult.success();
    }

    /**
     * 获取app图文和视频介绍
     * @return
     * @author wumin
     */
    @GetMapping("/getIntroduce")
    public Object getIntroduce(
            HttpSession session, String configKey) {
        IntroduceVo introduce = configService.getIntroduce(IntroduceVo.CONFIG_KEY);
        return MessageResult.success(introduce);
    }


    /**
     * app形象广告添加
     * @return
     * @author wumin
     */
    @Log(title = "app形象广告", operate = OperatorType.DATA_SAVEORUPDATE, channel = ChannelType.WEB_INTER)
    @PostMapping("/setApplicationFigure")
    public Object setApplicationFigure(
            HttpSession session, ApplicationFigure applicationFigure) {

        applicationFigure.setCreateTime(new Date());
        applicationFigure.setUpdateTime(new Date());
        configService.setApplicationFigure(applicationFigure);
        return MessageResult.success();
    }

    /**
     * 获取app形象广告
     * @return
     * @author wumin
     */
    @GetMapping("/getApplicationFigure")
    public Object getApplicationFigure(
            HttpSession session, String appId) {

        ApplicationFigure applicationFigure = configService.getApplicationFigure(ApplicationFigure.CONFIG_KEY+appId);
        return MessageResult.success(applicationFigure);
    }

    /**
     * 获取app形象广告列表
     * @return
     * @author wumin
     */
    @GetMapping("/applicationFigure/list")
    public Object applicationFigureList(
            @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
            @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {
        Page<AppApplication> pageInfo = new Page<>(page, limit);

        Page<JSONObject> pageData = configService.getApplicationFigureList(pageInfo, MapBuilder.newMap(1)
                .setParam("key",  ApplicationFigure.CONFIG_KEY + '%')
                .ok());
        pageData.setTotal(pageData.getRecords().size());

        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, pageData);
    }

    /**
     * 删除app形象广告
     * @return
     * @author wumin
     */
    @Log(title = "app形象广告", operate = OperatorType.DATA_DELETE, channel = ChannelType.WEB_INTER)
    @PostMapping("/delApplicationFigure")
    public Object delApplicationFigure(
            HttpSession session, String appId) {

        configService.delApplicationFigure(ApplicationFigure.CONFIG_KEY+appId);
        return MessageResult.success();
    }


    /**
     * 关于我们
     * @return
     * @author wumin
     */
    @Log(title = "关于我们", operate = OperatorType.DATA_SAVEORUPDATE, channel = ChannelType.WEB_INTER)
    @PostMapping("/setAboutUs")
    public Object setAboutUs(
            HttpSession session, AboutUsVo aboutUsVo) {

        configService.setAboutUs(aboutUsVo);
        return MessageResult.success();
    }

    /**
     * 关于我们
     * @return
     * @author wumin
     */
    @GetMapping("/getAboutUs")
    public Object getAboutUs(
            HttpSession session) {

        AboutUsVo aboutUsVo = configService.getAboutUs();
        return MessageResult.success(aboutUsVo);
    }


}
